package com.itheima.huikebackend.controller;

import com.itheima.huikebackend.common.PageResult;
import com.itheima.huikebackend.common.R;
import com.itheima.huikebackend.common.Result;
import com.itheima.huikebackend.dto.DicCreateDTO;
import com.itheima.huikebackend.dto.DictListDTO;
import com.itheima.huikebackend.service.DictionaryService;
import com.itheima.huikebackend.vo.SysDictDataVO;
import com.itheima.huikebackend.vo.SysDictListVO;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "字典相关接口")
@RequiredArgsConstructor
@RequestMapping("system/dict")
public class DictionaryController {

    private final DictionaryService dictionaryService;

    /**
     * 列表
     *
     * @param dictListDTO dict dto列表
     * @return {@link Result}<{@link PageResult}>
     */
    @GetMapping("/type/list")
    public R<Map<String,Object>> list(DictListDTO dictListDTO){
        Map map = dictionaryService.list(dictListDTO);
        return R.success(map);
    }

    /**
     * 查询类型
     *
     * @param type 类型
     * @return {@link Result}<{@link List}<{@link SysDictDataVO}>>
     */
    @GetMapping("data/type/{type}")
    public Result<List<SysDictDataVO>> queryType(@PathVariable String type){
        List<SysDictDataVO> list = dictionaryService.queryType(type);
        return Result.success(list);
    }

    /**
     * 查询通过id
     *
     * @param dictId dict id
     * @return {@link Result}<{@link SysDictListVO}>
     */
    @GetMapping("/type/{dictId}")
    public Result<SysDictListVO> queryById(@PathVariable Long dictId){
        SysDictListVO sysDictListVO = dictionaryService.queryById(dictId);
        return Result.success(sysDictListVO);

    }

    /**
     * 查询类型列表
     *
     * @param dictListDTO dict dto列表
     * @return {@link R}<{@link Map}<{@link String},{@link Object}>>
     */
    @GetMapping("/data/list")
    public  R<Map<String,Object>> queryTypeList(DictListDTO dictListDTO){
        Map map = dictionaryService.queryTypeList(dictListDTO);
        return R.success(map);
    }

    /**
     * 创建
     *
     * @param dicCreateDTO dic创建dto
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("type")
    public Result<?> create(@RequestBody DicCreateDTO dicCreateDTO){
        dictionaryService.create(dicCreateDTO);
        return Result.success();
    }

    /**
     * 删除
     *
     * @param ids dict id
     * @return {@link Result}<{@link ?}>
     */
    @DeleteMapping("/type/{ids}")
    public Result<?>delete(@PathVariable List<Long> ids){
        dictionaryService.delete(ids);
        return Result.success();
    }

    /**
     * 更新
     *
     * @param dicCreateDTO dic创建dto
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping("/type")
    public Result<?> update(@RequestBody DicCreateDTO dicCreateDTO){
        dictionaryService.update(dicCreateDTO);
        return Result.success();
    }

    /**
     * 出口
     *
     * @param response 响应
     */
    @GetMapping("/type/export")
    public void export(HttpServletResponse response){

        dictionaryService.export(response);
    }

}
